package com.rmt.www.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import com.rmt.www.constant.Priority;
import com.rmt.www.dao.DesignArtifactDao;
import com.rmt.www.dao.DesignArtifactHistoryDao;
import com.rmt.www.dao.DesignArtifactRequestDao;
import com.rmt.www.dao.FeatureArtifactDao;
import com.rmt.www.dao.FeatureArtifactHistoryDao;
import com.rmt.www.dao.FeatureArtifactRequestDao;
import com.rmt.www.dao.ImplementationArtifactDao;
import com.rmt.www.dao.ImplementationArtifactHistoryDao;
import com.rmt.www.dao.ImplementationArtifactRequestDao;
import com.rmt.www.dao.ProjectDao;
import com.rmt.www.dao.RequirementArtifactDao;
import com.rmt.www.dao.RequirementArtifactHistoryDao;
import com.rmt.www.dao.RequirementArtifactRequestDao;
import com.rmt.www.dao.TestArtifactDao;
import com.rmt.www.dao.TestArtifactHistoryDao;
import com.rmt.www.dao.TestArtifactRequestDao;
import com.rmt.www.service.AdminManageProjectService;
import com.rmt.www.vo.DesignArtifact;
import com.rmt.www.vo.FeatureArtifact;
import com.rmt.www.vo.ImplementationArtifact;
import com.rmt.www.vo.Project;
import com.rmt.www.vo.RequirementArtifact;
import com.rmt.www.vo.TestArtifact;

public class AdminManageProjectServiceImpl implements AdminManageProjectService {

	private ProjectDao projectDao;
	private DesignArtifactHistoryDao designArtifactHistoryDao;
	private FeatureArtifactHistoryDao featureArtifactHistoryDao;
	private RequirementArtifactHistoryDao requirementArtifactHistoryDao;
	private TestArtifactHistoryDao testArtifactHistoryDao;
	private ImplementationArtifactHistoryDao implementationArtifactHistoryDao;
	private FeatureArtifactDao featureArtifactDao;
	private FeatureArtifactRequestDao featureArtifactRequestDao;
	private RequirementArtifactDao requirementArtifactDao;
	private RequirementArtifactRequestDao requirementArtifactRequestDao;
	private DesignArtifactRequestDao designArtifactRequestDao;
	private DesignArtifactDao designArtifactDao;
	private ImplementationArtifactDao implementationArtifactDao;
	private ImplementationArtifactRequestDao implementationArtifactRequestDao;
	private TestArtifactDao testArtifactDao;
	private TestArtifactRequestDao testArtifactRequestDao;
	
	public void addNewProject(Project newProject) {
		projectDao.insert(newProject);
	}

	public void deleteProjectsByIdList(List<Integer> idList) {
		for (int id : idList) {
			projectDao.deleteProject(id);
		}
	}

	public List<Project> getProjectList() {
		List<Project> projectList = projectDao.getList();
		for (int i = 0; i < projectList.size(); i++) {
		
			List<Integer> _artifactNumList = new ArrayList<Integer>();
			List<Integer> _requestNumList  = new ArrayList<Integer>();
			List<Integer> _historyNumList  = new ArrayList<Integer>();
			List<Integer> _effortNumList  = new ArrayList<Integer>();
			List<Integer> _priorityHighList  = new ArrayList<Integer>();
			Project p = projectList.get(i);
			int pid = p.getPid();
			_artifactNumList.add(featureArtifactDao.getFeatureArtifactsByPId(pid).size());		
			_artifactNumList.add(requirementArtifactDao.getRequirementArtifactByPId(pid).size());
			_artifactNumList.add(designArtifactDao.getDesignArtifactsByPId(pid).size());
			_artifactNumList.add(implementationArtifactDao.getImplementationArtifactsByPId(pid).size());
			_artifactNumList.add(testArtifactDao.getTestArtifactsByPId(pid).size());
			
			_requestNumList.add(featureArtifactRequestDao.getFeatureArtifactRequestsByPId(pid).size());
			_requestNumList.add(requirementArtifactRequestDao.getRequirementArtifactRequestsByPId(pid).size());
			_requestNumList.add(designArtifactRequestDao.getDesignArtifactRequestByPId(pid).size());
			_requestNumList.add(implementationArtifactRequestDao.getImplementationArtifactRequestsByPId(pid).size());
			_requestNumList.add(testArtifactRequestDao.getTestArtifactRequestsByPId(pid).size());
			
			
			for (int j = 0; j < _requestNumList.size(); j++) 
			{
				if (_requestNumList.get(j) > 0)
				{
					p.set_hasrequest(true);
				}
			}
			
			//_historyNumList.add(featureArtifactHistoryDao.getFeatureArtifactHistoriesById(aid))
			int sum_effort = 0;
			int num_highpriority = 0;
			List<FeatureArtifact> list = featureArtifactDao.getFeatureArtifactsByPId(pid);
			for (int j = 0; j < list.size(); j++) 
			{
				sum_effort += list.get(j).getEffort();
				if (Priority.HIGH.equalsIgnoreCase(list.get(j).getPriority())) 
				{
					num_highpriority ++;
				}
			}
			_effortNumList.add(sum_effort);
			_priorityHighList.add(num_highpriority);
			sum_effort = 0;
			num_highpriority = 0;
			List<RequirementArtifact> list2 = requirementArtifactDao.getRequirementArtifactByPId(pid);
			for (int j = 0; j < list2.size(); j++) {
				sum_effort += list2.get(j).getEffort();
				if (Priority.HIGH.equalsIgnoreCase(list.get(j).getPriority())) 
				{
					num_highpriority ++;
				}
			}
			_effortNumList.add(sum_effort);
			_priorityHighList.add(num_highpriority);
			
			sum_effort = 0;
			num_highpriority = 0;
			List<DesignArtifact> list3 = designArtifactDao.getDesignArtifactsByPId(pid);
			for (int j = 0; j < list3.size(); j++) 
			{
				sum_effort += list3.get(j).getEffort();
				if (Priority.HIGH.equalsIgnoreCase(list.get(j).getPriority())) 
				{
					num_highpriority ++;
				}
			}
			_effortNumList.add(sum_effort);
			_priorityHighList.add(num_highpriority);
			sum_effort = 0;
			num_highpriority = 0;
			List<ImplementationArtifact> list4 = implementationArtifactDao.getImplementationArtifactsByPId(pid);
			for (int j = 0; j < list4.size(); j++) 
			{
				sum_effort += list4.get(j).getEffort();
				if (Priority.HIGH.equalsIgnoreCase(list.get(j).getPriority())) 
				{
					num_highpriority ++;
				}
			}
			_effortNumList.add(sum_effort);
			_priorityHighList.add(num_highpriority);
			sum_effort = 0;
			num_highpriority = 0;
			List<TestArtifact> list5 = testArtifactDao.getTestArtifactsByPId(pid);
			for (int j = 0; j < list5.size(); j++) {
				sum_effort += list5.get(j).getEffort();
				if (Priority.HIGH.equalsIgnoreCase(list.get(j).getPriority())) 
				{
					num_highpriority ++;
				}
			}
			_effortNumList.add(sum_effort);
			_priorityHighList.add(num_highpriority);
			
			projectList.get(i).set_artifactNumList(_artifactNumList);
			projectList.get(i).set_effortNumList(_effortNumList);
			projectList.get(i).set_requestNumList(_requestNumList);
			projectList.get(i).set_priorityHighList(_priorityHighList);
		}
		
		return projectList;
	}

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public DesignArtifactHistoryDao getDesignArtifactHistoryDao() {
		return designArtifactHistoryDao;
	}

	public void setDesignArtifactHistoryDao(
			DesignArtifactHistoryDao designArtifactHistoryDao) {
		this.designArtifactHistoryDao = designArtifactHistoryDao;
	}

	public FeatureArtifactHistoryDao getFeatureArtifactHistoryDao() {
		return featureArtifactHistoryDao;
	}

	public void setFeatureArtifactHistoryDao(
			FeatureArtifactHistoryDao featureArtifactHistoryDao) {
		this.featureArtifactHistoryDao = featureArtifactHistoryDao;
	}

	public RequirementArtifactHistoryDao getRequirementArtifactHistoryDao() {
		return requirementArtifactHistoryDao;
	}

	public void setRequirementArtifactHistoryDao(
			RequirementArtifactHistoryDao requirementArtifactHistoryDao) {
		this.requirementArtifactHistoryDao = requirementArtifactHistoryDao;
	}

	public TestArtifactHistoryDao getTestArtifactHistoryDao() {
		return testArtifactHistoryDao;
	}

	public void setTestArtifactHistoryDao(
			TestArtifactHistoryDao testArtifactHistoryDao) {
		this.testArtifactHistoryDao = testArtifactHistoryDao;
	}

	public ImplementationArtifactHistoryDao getImplementationArtifactHistoryDao() {
		return implementationArtifactHistoryDao;
	}

	public void setImplementationArtifactHistoryDao(
			ImplementationArtifactHistoryDao implementationArtifactHistoryDao) {
		this.implementationArtifactHistoryDao = implementationArtifactHistoryDao;
	}

	public FeatureArtifactDao getFeatureArtifactDao() {
		return featureArtifactDao;
	}

	public void setFeatureArtifactDao(FeatureArtifactDao featureArtifactDao) {
		this.featureArtifactDao = featureArtifactDao;
	}

	public FeatureArtifactRequestDao getFeatureArtifactRequestDao() {
		return featureArtifactRequestDao;
	}

	public void setFeatureArtifactRequestDao(
			FeatureArtifactRequestDao featureArtifactRequestDao) {
		this.featureArtifactRequestDao = featureArtifactRequestDao;
	}

	public RequirementArtifactDao getRequirementArtifactDao() {
		return requirementArtifactDao;
	}

	public void setRequirementArtifactDao(
			RequirementArtifactDao requirementArtifactDao) {
		this.requirementArtifactDao = requirementArtifactDao;
	}

	public RequirementArtifactRequestDao getRequirementArtifactRequestDao() {
		return requirementArtifactRequestDao;
	}

	public void setRequirementArtifactRequestDao(
			RequirementArtifactRequestDao requirementArtifactRequestDao) {
		this.requirementArtifactRequestDao = requirementArtifactRequestDao;
	}

	public DesignArtifactRequestDao getDesignArtifactRequestDao() {
		return designArtifactRequestDao;
	}

	public void setDesignArtifactRequestDao(
			DesignArtifactRequestDao designArtifactRequestDao) {
		this.designArtifactRequestDao = designArtifactRequestDao;
	}

	public DesignArtifactDao getDesignArtifactDao() {
		return designArtifactDao;
	}

	public void setDesignArtifactDao(DesignArtifactDao designArtifactDao) {
		this.designArtifactDao = designArtifactDao;
	}

	public ImplementationArtifactDao getImplementationArtifactDao() {
		return implementationArtifactDao;
	}

	public void setImplementationArtifactDao(
			ImplementationArtifactDao implementationArtifactDao) {
		this.implementationArtifactDao = implementationArtifactDao;
	}

	public ImplementationArtifactRequestDao getImplementationArtifactRequestDao() {
		return implementationArtifactRequestDao;
	}

	public void setImplementationArtifactRequestDao(
			ImplementationArtifactRequestDao implementationArtifactRequestDao) {
		this.implementationArtifactRequestDao = implementationArtifactRequestDao;
	}

	public TestArtifactDao getTestArtifactDao() {
		return testArtifactDao;
	}

	public void setTestArtifactDao(TestArtifactDao testArtifactDao) {
		this.testArtifactDao = testArtifactDao;
	}

	public TestArtifactRequestDao getTestArtifactRequestDao() {
		return testArtifactRequestDao;
	}

	public void setTestArtifactRequestDao(
			TestArtifactRequestDao testArtifactRequestDao) {
		this.testArtifactRequestDao = testArtifactRequestDao;
	}

	public void updateNewProject(Project project) {
		projectDao.updateProject(project);
	}

	public Project getProject(int pid) {
		return projectDao.getProject(pid);
	}

}
